package com.nitty.cash.loop.configs

import com.nitty.cash.loop.sdk.analyse.AnalyseBuilder
import com.nitty.cash.loop.sdk.analyse.AnalyseUtil
import com.nitty.cash.loop.utils.NittyDateUtil
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import org.json.JSONObject
import java.util.Random

/**
 * ApiUrl
 * @Author wwdeng
 * @CreateTime: 2024/12/5 15:48
 * @Annotation：
 */
object NittyApiUrl {

    /**
     * https://www.samwafin.online
     * http://159.138.165.72
     * 域名
     */
    const val NITTY_HOST = "http://159.138.165.72"

    /**
     * 地区转发
     * /v1/timezonePath
     */
    const val NITTY_TIMEZONE_URL = "/k4wKGCKrGYDrxgBZDOGA"

    /**
     * APP业务统一请求接口
     * /v1/commonApi
     */
    private val NITTY_COMMON_URLS = mutableListOf(
        "/lbfLpYdgBv5sTivcifDDBw",
        "/odZG2DUlACowUTHxntP5jw",
        "/xxhcv7fCg6FN4ldROpOQCg",
        "/fYPnlbiQMXR1oPS7A4B5Kg",
        "/w6wN0JjKpkF7479WCDWacw",
        "/kSdVEYU4UTCgxJW2KrmjOQ"
    )

    /**
     * 图片上传
     * /v1/imageUpload
     */
    const val NITTY_UPLOAD_FILE_URL = "/P0XgIbVSmVZSusLMUGd9Fw"

    /**
     * 获取请求Url
     */
    fun getHttpUrl(isUpload: Boolean = false,urlIndex: Int = -1): String{
        var url = getRandomUrl(urlIndex)
        if (isUpload){//上传
            url = NITTY_UPLOAD_FILE_URL
        }
        //地区转发
        val timeZone = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$NITTY_HOST/$timeZone$url"
        }
        return "$NITTY_HOST$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("w0148906f", AnalyseBuilder.product)                                           //  产品 APP名称
            put("na060", NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TOKEN))                                               //  token
            put("y2a2ba9d","v3")                                                      //  接口版本
            put("iff41937029f4", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("t9600a1efb81400f87c", AnalyseUtil.getAdId())                                          //  广告id
            put("v4b0790e4d", AnalyseUtil.getAdId())                                             //  广告id
            put("bec70", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("n6bcd30f2ab846f6bec", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("a843d49", NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_USERID))                                             //  userId
            put("version", AnalyseBuilder.libVersion)                                        //  app版本号
            put("timp", System.currentTimeMillis())                                            //  时间戳
            put("country", NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TIMEZONE_ID))                                          //当前时区id
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this.addCustomParams()
    }

    /**
     * 添加随机参数增量 4
     */
    private fun JSONObject.addCustomParams(): JSONObject {
        val mobile = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_MOBILE)
        val startTime = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_APP_START_TIME)
        val loginTime = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_LOGIN_TIME)
        val currentTime = NittyDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")

        put("loginMobile",mobile)
        val randomIndex = Random().nextInt(4)
        if (randomIndex == 1){
            apply {
                put("startTime",startTime)
            }
            return this
        }
        if (randomIndex == 2){
            apply {
                put("startTime",startTime)
                put("loginTime",loginTime)
            }
            return this
        }

        if (randomIndex == 3){
            apply {
                put("startTime",startTime)
                put("loginTime",loginTime)
                put("currentTime",currentTime)
            }
        }
        return this
    }

    /**
     * 获取随机Url
     */
    fun getRandomUrl(index: Int = -1): String{
        var urlIndex = index
        if (urlIndex == -1 || urlIndex > NITTY_COMMON_URLS.size - 1){
            urlIndex = Random().nextInt(NITTY_COMMON_URLS.size)
        }
        return NITTY_COMMON_URLS[urlIndex]
    }
}